<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="工种名称" class="movement">
          <el-input
            v-model="formInline.name"
            placeholder="请输入工种名称"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search()" class="movement"
            >查询</el-button
          >
          <el-button type="primary" @click="resert()" class="movement"
            >重置</el-button
          >
          <el-button type="primary" @click="addGongZhongs()"
            >新增工种</el-button
          >
        </el-form-item>
      </el-form>
      <!-- <el-button type="primary" class="divMia" @click="newly"
        >新增分店</el-button
      > -->
    </div>
    <div class="text item">
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%"
        max-height="600"
      >
        <el-table-column
          label="序号"
          align="center"
          type="index"
          :index="indexMethod"
          width="80"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          align="center"
          label="工种名称"
          width="180"
        >
        </el-table-column>

        <el-table-column prop="ctime" align="center" label="创建时间">
        </el-table-column>

        <el-table-column prop="status" align="center" label="状态">
          <template slot-scope="scope">
            <span>{{ scope.row.status == 1 ? "启用" : "禁用" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="240">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status == 1"
              type="danger"
              size="mini"
              @click="handleEdit(scope.row)"
              >禁用</el-button
            >
            <el-button
              v-if="scope.row.status == 2 || scope.row.status == 0"
              type="primary"
              size="mini"
              @click="handleEdit(scope.row)"
              >启用</el-button
            >
            <el-button
              v-if="scope.row.status == 2"
              type="danger"
              size="mini"
              @click="sanChu(scope.row)"
              >删除</el-button
            >
            <el-button
              v-if="scope.row.status == 2"
              type="warning"
              size="mini"
              @click="handleDelete(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[100, 200, 300, 400]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="titleText"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <span>
        <el-form
          ref="form"
          :model="formLabelAlign"
          label-width="90px"
          :rules="rules"
        >
          <div class="divWidth">
            <el-form-item label="工种名称" prop="name">
              <el-input v-model="formLabelAlign.name"></el-input>
            </el-form-item>
          </div>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="save">确 认</el-button>
        <el-button @click="handleClose">返 回</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  uploadFiles,
  storeList,
  addShop,
  region,
  storeStatus,
  shopDetails,
  StoreDeletion,
  condition,
} from "../../../../utils/api.js";
import {
  getGongZhong,
  addGongZhong,
  editGongZhong,
  delGongZhong,
  QiYong,
} from "../../../../utils/main";
export default {
  data() {
    return {
      options1: [
        {
          id: "1",
          name: "材料费+工时费",
        },
        {
          id: "2",
          name: "工时费",
        },
      ],
      value: "",
      formInline: {
        name: "",
      },
      formLabelAlign: {
        name: "",
      },
      titleText: "新增工种",

      loading: false,
      token: sessionStorage.getItem("token"),
      dialogVisible: false,

      tableData: [],

      //当前页数
      page: 1,
      limit: 100,
      total: 0,
      currentPage: 1,
      status: 0,
      name: "",
      rules: {
        name: [{ required: true, message: "请输入工种", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 启用禁用接口
    conditionUser(id, status) {
      QiYong(this.token, id, status)
        .then((res) => {
          if (res.data.status) {
            this.getList();
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },

    // 获取工种管理列表
    async getList() {
      this.loading = true;
      let res = await getGongZhong(
        this.token,
        this.page,
        this.limit,
        this.name
      );
      if (res.data.status) {
        this.tableData = res.data.data.data || [];
        this.total = res.data.data.count;
        // console.log(this.total);
        // console.log(this.tableData, "55555");
        let timer = setTimeout(() => {
          this.loading = false;
          clearTimeout(timer);
        }, 400);
      } else {
        if (res.data.msg == "token失效,请重新进行登录") {
          this.$router.push("/login");
        }
        if (res.data.msg == "此账号没有权限") {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
        let timer = setTimeout(() => {
          this.$message.error(res.data.msg);
        }, 5000);
      }
    },
    search() {
      this.name = this.formInline.name;
      this.page = 1;
      this.getList();
    },
    resert() {
      this.formInline.name = "";
      this.name = "";
      this.getList();
    },
    //启用禁用
    handleEdit(row) {
      if (row.status == 0) {
        let status = 1;
        this.conditionUser(row.id, status);
      }
      if (row.status == 1) {
        let status = 2;
        this.conditionUser(row.id, status);
      }
      if (row.status == 2) {
        let status = 1;
        this.conditionUser(row.id, status);
      }
      // this.getList();
    },

    //新增
    addGongZhongs() {
      this.formLabelAlign = {};
      this.dialogVisible = true;
      this.titleText = "新增工种";
    },
    // 编辑
    handleDelete(row) {
      let obj = JSON.parse(JSON.stringify(row));
      this.formLabelAlign = obj;
      this.dialogVisible = true;
      this.titleText = "编辑工种";
    },

    // 排序
    indexMethod(index) {
      if (this.total < this.limit) {
        return this.total - index;
      } else {
        return this.total - (this.page - 1) * this.limit - index;
      }
    },

    //关闭
    handleClose() {
      // (this.imgShowList = []), (this.imgShowForm = []), (this.form = {});
      // done();
      // this.regions1();
      this.$refs.form.resetFields();
      this.dialogVisible = false;
      // this.getList();
    },
    //删除
    sanChu(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // console.log(row.id);
          delGongZhong(this.token, row.id).then((res) => {
            if (res.data.status) {
              this.$message({
                type: "success",
                message: "删除成功",
              });
              this.getList();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //确定添加
    save() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // console.log(this.formLabelAlign, "8888880");
          if (this.formLabelAlign.id != null) {
            // console.log(this.formLabelAlign, "000");
            // 编辑
            editGongZhong(
              this.token,
              this.formLabelAlign.name,
              this.formLabelAlign.status,
              this.formLabelAlign.id
            ).then((res) => {
              if (res.data.status) {
                this.$message({
                  message: "修改成功",
                });
                this.dialogVisible = false;
                this.getList();
              } else {
                this.$message.error(res.data.msg);
              }
            });
          } else {
            // console.log(this.formLabelAlign, "111");

            // 新增
            addGongZhong(
              this.token,
              this.formLabelAlign.name,
              this.status
            ).then((res) => {
              if (res.data.status) {
                this.$message({
                  message: "新增成功",
                });
                this.total = res.data.data.count;
                this.formLabelAlign = {};
                this.dialogVisible = false;
                this.getList();
              } else {
                this.$message.error(res.data.msg);
              }
            });
          }
        } else {
          return false;
        }
      });
    },

    //页数
    handleSizeChange(val) {
      this.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getList();
    },

    handleRemove() {},
    //这是我自定义的移除事件
    handleClosedelete(i) {
      // this.fileList.splice(i, 1);//删除上传的文件
    },
  },
  mounted() {
    // this.storeLists();
    // this.regions1();
    this.getList();
    // this.form.seller_id = this.$route.params.id;
  },
};
</script>

<style scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

/* .clearfix {
  text-align: right;
} */

.box-card {
  width: 100%;
}

.imageGood {
  margin: 0 10px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.divWidth {
  width: 70%;
}
</style>